#!/usr/bin/env python

import os

"""
in
  No.     Time        Source                Destination           Protocol Length Info
        1 0.000000    Tp-LinkT_c7:5b:e5     Broadcast             ARP      60     Gratuitous ARP for 10.1.200.65 (Request)

  Frame 1: 60 bytes on wire (480 bits), 60 bytes captured (480 bits)
  Ethernet II, Src: Tp-LinkT_c7:5b:e5 (54:e6:fc:c7:5b:e5), Dst: Broadcast (ff:ff:ff:ff:ff:ff)
  Address Resolution Protocol (request/gratuitous ARP)

out
  1 0.000000    Tp-LinkT_c7:5b:e5     Broadcast             ARP      60     Gratuitous ARP for 10.1.200.65 (Request)
"""
def grep(inf, outf, expr):
  comando = 'cat', inf, '| grep', expr, '>', outf
  comando = ' '.join(comando)
  os.system(comando)
  
"""
in
   1 0.000000    Tp-LinkT_c7:5b:e5     Broadcast             ARP      60     Gratuitous ARP for 10.1.200.65 (Request)
   2 11.734686   IntelCor_97:7b:67     Broadcast             ARP      60     Who has 10.1.100.254?  Tell 10.1.100.8
  42 232.820794  IntelCor_7f:6c:fc     IntelCor_1e:c2:2e     ARP      60     10.1.11.254 is at 00:1b:21:7f:6c:fc
out
  10.1.200.65 frec %
  10.1.100.8 frec %
  10.1.11.254 frec %
"""
def ip_senders(inf, outf):
  # files
  infile = open(inf)
  outfile = open(outf, 'w')

  # header
  outfile.write('ip frec porcen\n')
  
  # loop to count
  
  # get ip
  ips = {}
  total = 0.0  # float, sino porcen queda en 0.0
  offset = 6
  for l in infile:
    line = l.split()
    total += 1
    if 'Gratuitous' in l:
      ip = line[offset+3]
    elif 'Who has' in l:
      ip = line[offset+4]
    elif 'is at' in l:
      ip = line[offset+0]
    else:
      print 'error_ip', 'line', line[0]
      exit()
    
    # dump into dict
    if ip not in ips:
      ips[ip] = 1
    else:
      ips[ip] += 1
  
  # sort by cant
  import operator
  iplist = sorted(ips.iteritems(), key = operator.itemgetter(1), reverse=True)
  
  tops = iplist[0:12]
  rest = iplist[12:]
  
  acum = 0
  for ip, cant in rest:
    acum += cant
  
  otros = ('otros', acum, round(acum/total*100, 2))
  
  # new list
  ips = []
  for ip, cant in tops:
    temp = (ip, cant, round(cant/total*100, 2))
    ips.append(temp)
  ips = ips + [otros]
  
  # dump into outfile
  for ip, cant, porcen in ips:
    outfile.write('%s %s %s\n' % (ip, str(cant), (str(porcen)+'%')))

  # close
  infile.close()
  outfile.close()

"""
in
   1 0.000000    Tp-LinkT_c7:5b:e5     Broadcast             ARP      60     Gratuitous ARP for 10.1.200.65 (Request)
   2 11.734686   IntelCor_97:7b:67     Broadcast             ARP      60     Who has 10.1.100.254?  Tell 10.1.100.8
  42 232.820794  IntelCor_7f:6c:fc     IntelCor_1e:c2:2e     ARP      60     10.1.11.254 is at 00:1b:21:7f:6c:fc
out
  Tp-LinkT frec %
  IntelCor frec %
  IntelCor frec %
"""
def vendors(inf, outf):
  # files
  infile = open(inf)
  outfile = open(outf, 'w')

  # header
  outfile.write('ip frec porcen\n')
  
  # loop to count
  
  # get ip
  vendors = {}
  total = 0.0  # float, sino porcen queda en 0.0
  offset = 2
  for l in infile:
    line = l.split()
    total += 1
    
    # IntelCor_7f:6c:fc
    vendor = line[offset]
    split = vendor.split('_')
    name = split[0]
    
    # dump into dict
    if name not in vendors:
      vendors[name] = 1
    else:
      vendors[name] += 1
  
  # sort by cant
  import operator
  venlist = sorted(vendors.iteritems(), key = operator.itemgetter(1), reverse=True)
  
  tops = venlist[0:10]
  rest = venlist[10:]
  
  acum = 0
  for name, cant in rest:
    acum += cant
  
  otros = ('otros', acum, round(acum/total*100, 2))
  
  # new list
  vendors = []
  for name, cant in tops:
    temp = (name, cant, round(cant/total*100, 2))
    vendors.append(temp)
  vendors = vendors + [otros]
  
  # dump into outfile
  for name, cant, porcen in vendors:
    outfile.write('%s %s %s\n' % (name, str(cant), (str(porcen)+'%')))

  # close
  infile.close()
  outfile.close()

### main

#~ grep('data.0', 'data.1', '" ARP "')
#~ ip_senders('data.1', 'ip.frec')
vendors('data.1', 'vendors.frec')

